#include <iostream>
#include "TcpServer.hpp"
#include <memory>
#include "Socket.hpp"
#include <string>
#include "Connection.hpp"
#include "SetNonBlock.hpp"
#include "Accepter.hpp"

void test(Connection* conn)
{   
    std::cout << "测试" << std::endl;       //可以成功注册  
}
int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        lg.LogMessage(Error, "输入格式：./PollServer serverport\n");
        return usage_err;
    }
    // 1.创建服务器
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>();

    // 2.创建一个listensockfd
    std::unique_ptr<Socket> listensock = std::make_unique<TcpSocket>();
    listensock->CreateListenSocket(std::stoi(argv[1]));
    SetNonBlock(listensock->GetSockfd());   //设置成非阻塞
    //不用test了，专门给接收链接定义了一个类
    // std::unique_ptr<Accepter> accepter = std::make_unique<Accepter>();
    // func_t func = std::bind(&Accepter::AccepterConnection, accepter.get(), std::placeholders::_1);

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(std::stoi(argv[1]));
    sockaddr.sin_addr.s_addr = INADDR_ANY;
    Connection *conn = ConnectionFactory::CreateListenConnection(listensock->GetSockfd(),
                                                                 EPOLLIN | EPOLLET,
                                                                 Accepter::AccepterConnection,
                                                                 tsvr.get(),
                                                                 sockaddr);
    //2.tcpserver管理链接
    tsvr->AddConnction(conn);
    //3.启动
    tsvr->Dispatch();
    return 0;
}